<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'quickSearch']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('xmwechat.offiaccount.autoreply.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <el-tabs type="border-card" v-model="state.defaultType" @tab-change="typeChange">
            <div v-for="(item, index) in state.type" :key="index">
                <el-tab-pane :label="item.label" :name="item.name">
                    <Table ref="tableRef"></Table>
                </el-tab-pane>
            </div>
        </el-tabs>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

defineOptions({
    name: 'xmwechat/offiaccount/autoreply',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/xmwechat.offiaccount.Autoreply/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            {
                label: t('xmwechat.offiaccount.autoreply.keyword'),
                prop: 'keyword',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
            },
            {
                label: t('xmwechat.offiaccount.autoreply.type'),
                prop: 'type',
                align: 'center',
                render: 'tag',
                operator: '=',
                sortable: false,
                replaceValue: {
                    follow: t('xmwechat.offiaccount.autoreply.type follow'),
                    keyword: t('xmwechat.offiaccount.autoreply.type keyword'),
                    default: t('xmwechat.offiaccount.autoreply.type default'),
                },
            },
            {
                label: t('xmwechat.offiaccount.autoreply.msg_type'),
                prop: 'msg_type',
                align: 'center',
                render: 'tag',
                operator: '=',
                sortable: false,
                replaceValue: {
                    text: t('xmwechat.offiaccount.autoreply.msg_type text'),
                    image: t('xmwechat.offiaccount.autoreply.msg_type image'),
                    voice: t('xmwechat.offiaccount.autoreply.msg_type voice'),
                    video: t('xmwechat.offiaccount.autoreply.msg_type video'),
                    news: t('xmwechat.offiaccount.autoreply.msg_type news'),
                },
            },
            {
                label: t('xmwechat.offiaccount.autoreply.status'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                operator: '=',
                sortable: false,
                replaceValue: { '0': t('xmwechat.offiaccount.autoreply.status 0'), '1': t('xmwechat.offiaccount.autoreply.status 1') },
            },
            {
                label: t('xmwechat.offiaccount.autoreply.reply_content'),
                prop: 'reply_content',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
            },
            {
                label: t('xmwechat.offiaccount.autoreply.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('xmwechat.offiaccount.autoreply.update_time'),
                prop: 'update_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: { type: 'keyword', msg_type: 'text', status: '1' },
    }
)

provide('baTable', baTable)

const state = reactive({
    defaultType: 'keyword',
    type: [
        { label: '关键词回复', name: 'keyword' },
        { label: '关注回复', name: 'follow' },
        { label: '默认回复', name: 'default' },
    ],
})

const typeChange = (name: any) => {
    state.defaultType = name
    baTable.table.filter!.search = [{ field: 'type', val: name, operator: '=' }]
    baTable.getIndex()
    if (state.defaultType == 'keyword') {
        baTable.table.column[1].show = true
    } else {
        baTable.table.column[1].show = false
    }
}

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.table.filter!.search = [{ field: 'type', val: state.defaultType, operator: '=' }]
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss"></style>
